package cn.xn.hrms.api.controller;

import cn.xn.hrms.api.dto.res.LeaveDTO;
import cn.xn.hrms.domain.service.LeaveService;
import cn.xn.hrms.domain.utils.ResultVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: ysz
 * @date: 2023/07/21
 **/

@RestController
@RequestMapping("/leave")
public class LeaveController {

    @Resource
    private LeaveService leaveService;


    /**
     * 创建请假条
     *
     * @param leaveDTO
     * @return
     */
    @ApiOperation("新增请假单")
    @PostMapping("/create")
    public ResultVo create(@RequestBody LeaveDTO leaveDTO) {
        leaveService.create(leaveDTO);
        return ResultVo.success();
    }

    /**
     * 分页查询请假单
     *
     * @param personalId
     * @param leaveType
     * @param leaveStatus
     * @return
     */
    @ApiOperation("分页查询请假单")
    @GetMapping("/paged-list")
    PageInfo<LeaveDTO> leavePagedList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "size", defaultValue = "10") Integer size,
                                      @RequestParam(required = false) Integer personalId,
                                      @RequestParam(required = false) Integer leaveType,
                                      @RequestParam(required = false) Integer leaveStatus
    ) {

        return leaveService.page(page, size, personalId, leaveType, leaveStatus);

    }

    /**
     * 删除请假单（支持批量）
     * @param ids
     * @return
     */
    @ApiOperation("删除请假单（支持批量）")
    @DeleteMapping("/delete")
    public ResultVo deleteLeave(@RequestBody List<Long> ids) {
        leaveService.delete(ids);
        return ResultVo.success();

    }

    /**
     * 修改请假单
     * @param leaveDTO
     * @return
     */
    @ApiOperation("修改请假单")
    @PutMapping("/update")
    public ResultVo leaveUpdate(@RequestBody LeaveDTO leaveDTO) {
        leaveService.update(leaveDTO);
        return ResultVo.success();

    }

    /**
     * 审核请假单
     *
     * @return
     */
    @ApiOperation("审核请假单")
    @PutMapping("/approve")
    public ResultVo approveLeave(@RequestBody LeaveDTO leaveDTO) {
        leaveService.approve(leaveDTO.getLeaveId(), leaveDTO.getLeaveStatus(), leaveDTO.getApproveRemark());
        return ResultVo.success();
    }

    /**
     * 请假单列表
     *
     * @return
     */
    @ApiOperation("请假单列表")
    @GetMapping("/list")
    public List<LeaveDTO> leaveList(@RequestParam(required = false) Integer leaveStatus) {
        return leaveService.listAll(leaveStatus);

    }



}
